//#include "../DWMRICore/Point3F.h"
//#include "../DWMRICore/Point3I.h"
//#include "../DWMRICore/VectorField.h"
//#include "../DWMRICore/Scalar.h"
//
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//
//#include <vector>
//
//using namespace std;
//
//char *src_pathname = NULL;
//char *dst_pathname = NULL;
//
//struct Element{
//	CVector3F dir;
//	CPoint3I pos;
//	float weight;
//};
//
//void PrintUsage()
//{
//	printf("Usage: DisplayVectorFields -src input_vector_file -dst output_vector_file\n");
//}
//
//int ParseArguments(const int argc, char *argv[])
//{
//	for (int i = 1; i < argc; ++i) {
//		if (strcmp(argv[i], "-src") == 0) {
//			src_pathname = argv[++i];
//		} else if (strcmp(argv[i], "-dst") == 0) {
//			dst_pathname = argv[++i];
//		} else {
//			return -1;
//		}
//	}
//
//	if (src_pathname == NULL || dst_pathname == NULL)
//		return -2;
//
//	return 0;
//}
//
//void ReadVectorFields(vector<vector<CVector3F> > &vectorFields, int &w, int &h, int &d)
//{
//	FILE *fp = fopen(src_pathname, "rb");
//
//	fread(&w, sizeof(int), 1, fp);
//	fread(&h, sizeof(int), 1, fp);
//	fread(&d, sizeof(int), 1, fp);
//
//	vectorFields.resize(w*h*d);
//
//	for (int i = 0; i < w * h * d; ++i) {
//		int count;
//		fread(&count, sizeof(int), 1, fp);
//
//		for (int j = 0; j < count; ++j) {
//			float v[3];
//			fread(v, sizeof(float), 3, fp);
//			vectorFields.at(i).push_back(CVector3F(v[0], v[1], v[2]));
//		}
//	}
//
//	fclose(fp);
//}
//
//int GetMaxSize(vector<vector<CVector3F> > &vectorFields)
//{
//	int size = 0;
//	for (uint i = 0; i < vectorFields.size(); ++i) {
//		if (vectorFields.at(i).size() > size)
//			size = vectorFields.at(i).size();
//	}
//	return size;
//}
//
//int main(int argc, char *argv[])
//{
//	if (ParseArguments(argc, argv) != 0) {
//		PrintUsage();
//		return 0;
//	}
//
//	vector<vector<CVector3F> > vectorFields;
//	int w, h, d;
//	ReadVectorFields(vectorFields, w, h, d);
//
//	int maxSize = GetMaxSize(vectorFields);
//	printf("max size = %d\n", maxSize);
//
//	CScalar *scalr = new CScalar();
//	scalr->CreateScalar(w, h, d);
//	for (uint j = 0; j < vectorFields.size(); ++j) {
//		scalr->SetScalar(j, vectorFields.at(j).size());
//	}
//	scalr->SaveScalarFile(dst_pathname);
//	delete scalr;
//
//	//for (int i = 0; i < maxSize; ++i) {
//	//	/* display each vector field */
//
//	//	CVectorField *result = new CVectorField();
//	//	result->CreateVectorField(w, h, d);
//
//	//	for (uint j = 0; j < vectorFields.size(); ++j) {
//	//		int size = vectorFields.at(j).size();
//	//		if (size > 0) {
//	//			int index = i < size ? i : (size-1);
//	//			result->SetVector(j, vectorFields.at(j).at(index));
//	//		}
//	//	}
//
//	//	char pathname[256];
//	//	sprintf(pathname, "%s_%d.nrrd", dst_pathname, i);
//	//	result->SaveVectorFieldFile(pathname);
//	//	delete result;
//	//}
//
//
//
//	return 0;
//}

#include <stdio.h>

#include "vtkPolyData.h"
#include "vtkPolyDataReader.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTubeFilter.h"
#include "vtkGenericDataObjectWriter.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkProperty.h"


int w = 100;
int h = 100;
int d = 100;

int main(int argc, char *argv[])
{
	float *vectors = new float[w*h*d*12];
	FILE *fp = fopen(argv[1], "rb");
	fread(vectors, sizeof(float), w*h*d*12, fp);
	fclose(fp);

	int y = 50;

	vtkPoints *pts = vtkPoints::New();
	vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
	colors->SetNumberOfComponents(3);
	colors->SetName("Colors");

	pts->Initialize();
	for (int z = 0; z < d; ++z) {
		for (int x = 0; x < w; ++x) {
			int index = (z * h + y) * w + x;
			float weight = vectors[index*12+3] * 0.4f;
			pts->InsertNextPoint(x+vectors[index*12+0]*weight, y+vectors[index*12+1]*weight, 
				z+vectors[index*12+2]*weight);
			pts->InsertNextPoint(x-vectors[index*12+0]*weight, y-vectors[index*12+1]*weight, 
				z-vectors[index*12+2]*weight);

			unsigned char color[3];
			color[0] = (unsigned char)(fabs(vectors[index*12+0]) * 255.0f);
			color[1] = (unsigned char)(fabs(vectors[index*12+1]) * 255.0f);
			color[2] = (unsigned char)(fabs(vectors[index*12+2]) * 255.0f);
			colors->InsertNextTupleValue(color);
			
			weight = vectors[index*12+7] * 0.4f;
			pts->InsertNextPoint(x+vectors[index*12+4]*weight, y+vectors[index*12+5]*weight, 
				z+vectors[index*12+6]*weight);
			pts->InsertNextPoint(x-vectors[index*12+4]*weight, y-vectors[index*12+5]*weight, 
				z-vectors[index*12+6]*weight);

			color[0] = (unsigned char)(fabs(vectors[index*12+4]) * 255.0f);
			color[1] = (unsigned char)(fabs(vectors[index*12+5]) * 255.0f);
			color[2] = (unsigned char)(fabs(vectors[index*12+6]) * 255.0f);
			colors->InsertNextTupleValue(color);

			weight = vectors[index*12+11] * 0.4f;
			pts->InsertNextPoint(x+vectors[index*12+8]*weight, y+vectors[index*12+9]*weight, 
				z+vectors[index*12+10]*weight);
			pts->InsertNextPoint(x-vectors[index*12+8]*weight, y-vectors[index*12+9]*weight, 
				z-vectors[index*12+10]*weight);

			color[0] = (unsigned char)(fabs(vectors[index*12+8]) * 255.0f);
			color[1] = (unsigned char)(fabs(vectors[index*12+9]) * 255.0f);
			color[2] = (unsigned char)(fabs(vectors[index*12+10]) * 255.0f);
			colors->InsertNextTupleValue(color);
		}
	}

	vtkPolyData *vectorData = vtkPolyData::New();
	vectorData->Initialize();
	vectorData->Allocate();
	vectorData->SetPoints(pts);
	int index = 0;
	for (int z = 0; z < d; ++z) {
		for (int x = 0; x < w; ++x) {
			vtkIdType connectivity[2];
			
			connectivity[0] = index;
			connectivity[1] = index + 1;
			vectorData->InsertNextCell(VTK_LINE, 2, connectivity);
			index += 2;

			connectivity[0] = index;
			connectivity[1] = index + 1;
			vectorData->InsertNextCell(VTK_LINE, 2, connectivity);
			index += 2;

			connectivity[0] = index;
			connectivity[1] = index + 1;
			vectorData->InsertNextCell(VTK_LINE, 2, connectivity);
			index += 2;
		}
	}
	vectorData->GetCellData()->SetScalars(colors);
	vectorData->Update();

	//vtkGenericDataObjectWriter* writer = vtkGenericDataObjectWriter::New();
	//writer->SetFileName("C:\\Ziang\\Dataset\\Phantom\\SNR10\\Nrrd_2X\\vectors.vtk");
	//writer->SetInput(vectorData);          //polydata is my polydata object
	//writer->Update();
	//writer->Write();

	delete[] vectors;

    vtkPolyDataMapper *pPolyDataMapper = vtkPolyDataMapper::New();
	pPolyDataMapper->SetInput(vectorData);

    vtkActor *pActor = vtkActor::New();
    pActor->SetMapper(pPolyDataMapper);

    vtkRenderer *pRenderer = vtkRenderer::New();
    pRenderer->AddActor(pActor);

    vtkRenderWindow *pRenderWindow = vtkRenderWindow::New();
    pRenderWindow->AddRenderer(pRenderer);
    pRenderWindow->SetSize(1024, 1024);

    vtkRenderWindowInteractor *pRenderWindowInteractor = vtkRenderWindowInteractor::New();
    pRenderWindowInteractor->SetRenderWindow(pRenderWindow);

    pRenderWindow->Render();
    pRenderWindowInteractor->Start();
}
